package com.twsm.szjy.oa.routine.action;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.text.ExtendedMessageFormat;
import org.apache.log4j.Logger;

public class ActionServlet extends HttpServlet {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ActionServlet.class);

	/**
	 * 缓存反射过的方法 TODO : 替换成google并发缓存Map LRU
	 */
	private static HashMap<String, Method> cacheServiceMethod = new HashMap<String, Method>();

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * Service层反射路径
	 */
	private static final String SERVICE_PATH = "com.twsm.szjy.oa.routine.service.";

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		if (logger.isDebugEnabled()) {
			logger.debug(ExtendedMessageFormat.format("ServletPath  [{0}]",
					req.getServletPath()));
		}

		String actionPath = req.getServletPath();

		if (actionPath.indexOf("#") < -1) {
			throw new ServletException(ExtendedMessageFormat.format(
					"不符合定义的ServletPath  [{0}]", actionPath));
		}

		String serviceClazz = actionPath.split("#")[0];
		String serviceMethod = actionPath.split("#")[1];

		// 如果缓存中没有有保存过
		if (!cacheServiceMethod.containsKey(actionPath)) {
			try {
				Class.forName(SERVICE_PATH + serviceClazz);
			} catch (ClassNotFoundException e) {
				throw new ServletException(ExtendedMessageFormat.format(
						"不存在的Service [{0}]", serviceClazz));
			}
			
			
			
		}

		/**
		 * TODO: getServletPath = => UserService#login service#方法
		 * 
		 * 这里做一个申请集中转发的到Service 层的反射
		 * 
		 * 然后通过自定义的标注来决定 Service 层执行完后 跳转的页面是什么
		 * 
		 * 
		 */
	}

}
